#include "asynch_read_queue.h"
#include "asynch_return_queue.h"
#include "srv_asynch_man.h"
Asynch_Read_Queue::Asynch_Read_Queue(Srv_Asynch_Man *pAsynch_man):m_pAsynch_man(pAsynch_man)
{

}

Asynch_Read_Queue::~Asynch_Read_Queue()
{
    close();
}

void Asynch_Read_Queue::arq_post_read_socket(int client_socket)
{
    std::string str_buf;
    str_buf.append((char*)&client_socket,sizeof(client_socket));  //加入buf前几个字节 附加结构
    putq(str_buf);		//入队
}

int Asynch_Read_Queue::close()
{
    std::string str("");
    int nthread  =thr_count();

    for (int i =0 ;i<nthread ;i++)
        putq(str);

    return 0;
}

int Asynch_Read_Queue::svc()
{
    std::string str_buf ="";

    std::string str ="";

    while(getq(str))	//出队
    {
        if (str=="")	//为空为退出标志
            return true;

        const char *pSocket = str.data();

        int * Socket = (int *)pSocket ;

        if(m_pAsynch_man)
        {
            str_buf.clear();
            m_pAsynch_man->handle_read_passive(*Socket,str_buf);//主动读取socket buf

            if(!str_buf.empty())
                m_pAsynch_man->append_read_buf(*Socket,str_buf);    //put buf 到队列
        }


    }
    return  0;
}
